package com.saasovation.agilepm.application.sprint;

import com.saasovation.agilepm.domain.model.product.backlogitem.*;
import com.saasovation.agilepm.domain.model.product.sprint.*;
import com.saasovation.agilepm.domain.model.tenant.TenantId;

/**
 * 冲刺应用服务
 * @author learn
 */
public class SprintApplicationService {

    /**
     * 待办事项存储库
     */
    private BacklogItemRepository backlogItemRepository;
    /**
     * 冲刺存储库
     */
    private SprintRepository sprintRepository;

    /**
     * 由以下参数构造冲刺应用服务
     * @param aSprintRepository         冲刺存储库
     * @param aBacklogItemRepository    待办事项存储库
     */
    public SprintApplicationService(
            SprintRepository aSprintRepository,
            BacklogItemRepository aBacklogItemRepository) {

        // 调用 父类默认构造器
        super();

        this.backlogItemRepository = aBacklogItemRepository;
        this.sprintRepository = aSprintRepository;
    }

    /**
     * 提交待办事项到冲刺
     * @param aCommand                  将待办事项提交给冲刺命令
     */
    public void commitBacklogItemToSprint(
            CommitBacklogItemToSprintCommand aCommand) {

        // 从冲刺命令构造租户ID
        TenantId tenantId = new TenantId(aCommand.getTenantId());

        // 从冲刺存储库获取冲刺
        Sprint sprint =
                this.sprintRepository()
                    .sprintOfId(
                            tenantId,
                            new SprintId(aCommand.getSprintId()));
        // 从待办事项存储库获取待办事项
        BacklogItem backlogItem =
                this.backlogItemRepository()
                    .backlogItemOfId(
                            tenantId,
                            new BacklogItemId(aCommand.getBacklogItemId()));
        // 冲刺来提交待办事项
        sprint.commit(backlogItem);
        // 冲刺存储库来保存：冲刺
        this.sprintRepository().save(sprint);
    }

    /**
     * 获取待办事项存储库
     * @return                          返回待办事项存储库
     */
    private BacklogItemRepository backlogItemRepository() {
        return this.backlogItemRepository;
    }

    /**
     * 获取冲刺存储库
     * @return                          返回冲刺存储库
     */
    private SprintRepository sprintRepository() {
        return this.sprintRepository;
    }
}
